﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace svs.Mobile.BeyondMedia.UiCore
{
	public partial class MessageBoxEx : Form
	{
		private DialogResult _CancelResult;
		private DialogResult _OKResult;
		private DialogResult _Result = DialogResult.None;

		public MessageBoxEx(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
		{
			InitializeComponent();

			Text = caption;
			DialogText.Text = text;

			switch (buttons)
			{
				case MessageBoxButtons.OK:
					mnuOK.Text = "OK";
					_OKResult = DialogResult.OK;
					break;

				case MessageBoxButtons.OKCancel:
					mnuOK.Text = "OK";
					mnuCancel.Text = "Cancel";
					DialogMenu.MenuItems.Add(mnuCancel);
					_OKResult = DialogResult.OK;
					_CancelResult = DialogResult.Cancel;
					break;

				case MessageBoxButtons.YesNo:
					mnuOK.Text = "Yes";
					mnuCancel.Text = "No";
					DialogMenu.MenuItems.Add(mnuCancel);
					_OKResult = DialogResult.Yes;
					_CancelResult = DialogResult.No;
					break;
			}

			DialogResult = DialogResult.None;
		}

		public static DialogResult Show(string text, string caption)
		{
			return MessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
		}

		public static DialogResult Show(string text)
		{
			return MessageBox.Show(text,"BeyondPod", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
		}

		public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
		                                MessageBoxDefaultButton defaultButton)
		{
			if (text == null || caption == null)
				return DialogResult.Ignore;

			if (PlatformDetection.IsSmartphone() || text.Length < 130)
				return MessageBox.Show(text, caption, buttons, icon, defaultButton);
			else
			{
				MessageBoxEx dlg = new MessageBoxEx(text, caption, buttons, icon);
				return dlg.ShowDialog();
			}
		}

		private void OnOK(object sender, EventArgs e)
		{
			DialogResult = _OKResult;
		}

		private void OnCancel(object sender, EventArgs e)
		{
			DialogResult = _CancelResult;
		}

		private void OnClosing(object sender, CancelEventArgs e)
		{
			//Closed using the OK button on the Pocket PC;
			if (DialogResult == DialogResult.None)
				DialogResult = _OKResult;
		}

		private void OnLoad(object sender, EventArgs e)
		{
			MessageBeep(32);
		}

		[DllImport("Coredll.dll")]
		private static extern void MessageBeep(int Flags);
	}
}